<?

class classSNMP {
	
	public $ip_address = '';
	public $version = 'v2c';
	public $community = 'public'; 

	private $walk = array();
	private $data = array();
	public function __construct($cfg=array()) {
		if (is_array($cfg) || is_object($cfg)) {
			foreach ($cfg AS $name=>$value) {
				switch ($name) {
					case 'ip_address':
					case 'community':
						$this->$name = $value;
						break;
					case 'version':
						switch ($value) {
							case 'v1':
							case '1':
								$this->version = 'v1';
								break;
							case 'v2':
							case 'v2c':
							case '2':
								$this->version = 'v2c';
								break;
							case 'v3':
							case '3':
								$this->version = 'v3';
								break;
						}
						break;
					case 'mibs':
						if (is_array($value)) {
							foreach ($value AS $path) {
								echo 'Loading MIB File: '.$path.chr(10);
								if (is_file($path)) {
									echo 'Found MIB File: '.$path.chr(10);
									snmp_read_mib($path);
								}
							}
						}
						break;
				}
			
			}
		}
		if (!empty($this->ip_address)) { $this->fetchData();}
	}
	public function processWalk($walk=array()) {
		if (is_array($walk) || is_object($walk)) {
			foreach ($walk AS $name=>$value) {
				list($section,$oid) = explode('::',$name);
				$this->walk[$section][$oid] = $this->sanitizeValue($value);
			}
		}
	}	
	
	public function setWalk($value=array()) {
		if (is_array($value) || is_object($value)) {
			$this->walk = $value;
		}
	}	
	public function getWalk() { return $this->walk;}
	public function clearWalk() { $this->walk = array();}
	public function walkLoaded() { return !empty($this->walk);}
	public function fetchData() {
		// retrive from device
		switch ($this->version) {
			case 'v1':
				$walk = @snmprealwalk($this->ip_address,$this->community,null);
				break;
			case 'v2c':
				$walk = @snmp2_real_walk($this->ip_address,$this->community,null);
				break;
			case 'v3':
				$walk = @snmp3_real_walk($this->ip_address,$this->community,null);			
				break;
		}
		if (is_array($walk)) {
			// Process for easier searching
			foreach ($walk AS $name=>$value) {
				list($section,$oid) = explode('::',$name);
				$this->walk[$section][$oid] = $this->sanitizeValue($value);		
			}
		}
		
	}
	
	
	public function getData($oid='') {
		// retrive from device

		switch ($this->version) {
			case 'v1':
				$walk = @snmprealwalk($this->ip_address,$this->community,$odi);
				break;
			case 'v2c':
				$walk = @snmp2_real_walk($this->ip_address,$this->community,$oid);
				break;
			case 'v3':
				$walk = @snmp3_real_walk($this->ip_address,$this->community,$oid);			
				break;
		}

		if (is_array($walk)) {
			foreach ($walk AS $name=>$value) {
				list($section,$oid) = explode('::',$name);
				$this->walk[$section][$oid] = $this->sanitizeValue($value);		
			}
			return true;
		} else return false;
		
	}

	public function load($path='') {
		$this->walk = array();
		if (is_file($path)) {
			$file = fopen($path,'r');
			while(!feof($file)) {
				$line = fgets($file);
				list($key,$value) = explode('=',$line);
				list($section,$oid) = explode('::',trim($key));
				if (!empty($section)) {
					$this->walk[$section][$oid] = trim($value);
				} else {
					$this->walk['Unknown'][$oid] = trim($value);
				}
			}
			fclose($file);
		} else echo 'File Not Found ('.$path.')';
	}
	
	
	private function sanitizeValue($value='') {
		if (preg_match('/Wrong Type/', $value)) { $value = trim(substr($value,strpos($value,':')+1));}
		if (preg_match('/^INTEGER:/', $value)) {
			$value = trim(substr($value,8));
			if (substr($value,0,2)=='up') {
				return 'up';
			} elseif (substr($value,0,4)=='down') {
				return 'down';
			} elseif (substr($value,0,5)=='false') {
				return false;
			} elseif (substr($value,0,4)=='true') {
				return true;
			} elseif (preg_match('/\(([0-9]+)\)/',$value)) {
				preg_match('/\(([0-9]+)\)/',$value,$matches);	
				return $matches[1];
			} elseif (preg_match('/^([0-9]+)/',$value)) {
				preg_match('/^([0-9]+)/',$value,$matches);	
				return $matches[1];

			} else {
				return $value;
			}
		} elseif (preg_match('/^Counter32:/', $value) || preg_match('/^Counter64:/', $value)) {
			return trim(substr($value,10));
		} elseif (preg_match('/^STRING:/', $value)) {
			return str_replace('"','',trim(substr($value,7)));		
		} elseif (preg_match('/^OID:/', $value)) {					
			return substr($value,strrpos($value,':')+1);
		} elseif (preg_match('/^Gauge32:/', $value)) {
			return trim(substr($value,8));
		} elseif (preg_match('/^Timeticks:/', $value)) {
			preg_match('/\(([0-9]+)\)/',$value,$timeticks);			
			return round($timeticks[1]*0.01,2);
		} else return $value;
	}
	
	public function find($section='',$search='',$removeIndex=false) {
		$output = array();
		if (empty($section)) {
			list($section,$search) = explode('::',$search);
		}
		if (empty($section)) {
			foreach ($this->walk AS $section=>$oids) {
				foreach ($oids AS $name=>$value) {
					if (preg_match('/'.$search.'/', $name)) {
						if ($removeIndex===true) {
							$pos = strrpos($name,'.');
							$output[substr($name,$pos+1)][substr($name,0,$pos)] = $this->sanitizeValue($value);
						} else {
							$output[$name] = $this->sanitizeValue($value);
						}
					}
				}
			}
		} else {
			if (isset($this->walk[$section])) {
				foreach ($this->walk[$section] AS $name=>$value) {
					if (preg_match('/'.$search.'/', $name)) {
						if ($removeIndex===true) {
							$pos = strrpos($name,'.');
							$output[substr($name,$pos+1)][substr($name,0,$pos)] = $this->sanitizeValue($value);
						} else {
							$output[$name] = $this->sanitizeValue($value);
						}
					}
				}
			}
		}
		return $output;
	}
	public function get($section='',$key='') {
		if (empty($key)) {
			// Assume passed full oid
			list($section,$key) = @explode('::',$section);
			if (!empty($section)) {
				return isset($this->walk[$section][$key])?$this->sanitizeValue($this->walk[$section][$key]):null;
			} else {
				return isset($this->walk[''][$key])?$this->sanitizeValue($this->walk[''][$key]):null;
			}
		} else {
			return isset($this->walk[$section][$key])?$this->sanitizeValue($this->walk[$section][$key]):null;
		}
	}
	public function exists($section,$key) { return isset($this->walk[$section][$key]);}

}


?>